package com.system.healthproject.exception;


import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.validation.ConstraintViolation;
import javax.validation.ConstraintViolationException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

@ControllerAdvice
public class GlobleExceptionHandler {



    /**
     * 表单校验异常处理
     * @param e
     * @return
     */
    @ExceptionHandler(ConstraintViolationException.class)
    @ResponseBody
    public Map<String,Object> constraintViolationException(ConstraintViolationException e){
        Set<ConstraintViolation<?>> constraintViolations = e.getConstraintViolations();
        StringBuffer buffer=new StringBuffer();
        for (ConstraintViolation<?> constraintViolation : constraintViolations) {
            String message = constraintViolation.getMessage();
            buffer.append(message+",");
        }
        String substring = buffer.substring(0, buffer.length() - 1);
        if(substring.equals("不能为空")){
            substring="参数不能为空";
        }else {
            substring=buffer.toString();
        }
        HashMap<String, Object> result = new HashMap<>();
        result.put("code",500);
        result.put("msg",substring);
        return result;
    }

    /**
     * json bean 校验异常处理
     * @param e
     * @return
     */
    @ExceptionHandler(MethodArgumentNotValidException.class)
    @ResponseBody
    public Map<String,Object> methodArgumentNotValidException(MethodArgumentNotValidException e){
        HashMap<String, Object> result = new HashMap<>();
        result.put("code",500);
        BindingResult bindingResult = e.getBindingResult();
        StringBuffer errorMesssage = new StringBuffer();
        List<FieldError> fieldErrors = bindingResult.getFieldErrors();
        for (FieldError fieldError : fieldErrors) {
            errorMesssage.append(fieldError.getDefaultMessage() + ",");
        }
        String substring = errorMesssage.substring(0, errorMesssage.length() - 1);
        if(substring.equals("不能为空")){
            substring="参数不能为空";
        }else {
            substring=errorMesssage.toString();
        }
        result.put("msg", substring);
        return result;
    }

    /**
     * 由@RequestBody 引发 如果加上了@RequestBody注解 而前端没有传递参数 就会抛出这个异常
     * @param e
     * @return
     */
    @ExceptionHandler(HttpMessageNotReadableException.class)
    @ResponseBody
    public Map<String,Object> totalException(HttpMessageNotReadableException e){
        HashMap<String, Object> result = new HashMap<>();
        result.put("code",500);
        result.put("msg", "参数不能为空");
        return result;
    }

    /**
     * 自定义token异常
     * @param e
     * @return
     */
    @ExceptionHandler(TokenException.class)
    @ResponseBody
    public Map<String,Object> tokenException(TokenException e){
        HashMap<String, Object> result = new HashMap<>();
        result.put("code",500);
        result.put("msg", e.getMessage());
        return result;
    }

    /**
     * 总异常处理
     * @param e
     * @return
     */
    @ExceptionHandler(Exception.class)
    @ResponseBody
    public Map<String,Object> totalException(Exception e){
        HashMap<String, Object> result = new HashMap<>();
        result.put("code",500);
        result.put("msg", "未定义异常");
        e.printStackTrace();
        return result;
    }
}
